﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for TopicDAL
/// </summary>
public class TopicDAL : DALBase
{

    #region CREATE


    public void NewTopic(OneTopic topic)
    {
        using (SqlConnection conn = CreateConnection())
        {
            try
            {               
                var cmd = new SqlCommand("dbo.usp_CreateTopic", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@TopicID", SqlDbType.Int, 4).Direction = ParameterDirection.Output;

                cmd.Parameters.Add("@Header", SqlDbType.VarChar, 150).Value = topic.Header;
                cmd.Parameters.Add("@TextContent", SqlDbType.VarChar, 2000).Value = topic.TextContent;

                cmd.Parameters.Add("@Author", SqlDbType.VarChar, 50).Value = topic.Author;
                cmd.Parameters.Add("@UserID", SqlDbType.Int, 4).Value = topic.UserID;
                
                conn.Open();
                cmd.ExecuteNonQuery();
                topic.TopicID = (Int32)cmd.Parameters["@TopicID"].Value; 
            }
            catch
            {
                throw new ApplicationException("An error has occured in the data access layer.");
            }

        }
    }
    

    #endregion


    #region READ

    #endregion


    #region UPDATE

    #endregion

    #region DELETE

    public void DeleteTopicOnID(int topicID)
    {
        using (SqlConnection conn = CreateConnection()) // using för att den alltid ska stängas sen.
        {
            try
            {
                var cmd = new SqlCommand("dbo.usp_UpdateTopicToDeleted", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@TopicID", SqlDbType.Int, 4).Value = topicID;
                cmd.Parameters.Add("@Deleted", SqlDbType.VarChar, 5).Value = "true";

                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch
            {
                throw new ApplicationException("An error has occured in the data access layer.");
            }
        }

    }

    #endregion





    public OneTopic GetTopicOnID(int topicID)
    {
        Sanitize sanitize = new Sanitize();
        using (SqlConnection conn = CreateConnection())
        {
            try
            {
                SqlCommand cmd = new SqlCommand("dbo.usp_GetTopicOnID", conn);
                cmd.CommandType = CommandType.StoredProcedure;


                cmd.Parameters.AddWithValue("@TopicID", topicID);

                conn.Open();


                using (SqlDataReader reader = cmd.ExecuteReader())
                {

                    if (reader.Read())
                    {

                        var UserIDIndex = reader.GetOrdinal("UserID");
                        var AuthorIndex = reader.GetOrdinal("Username");
                        var HeaderIndex = reader.GetOrdinal("Header");
                        var TextContentIndex = reader.GetOrdinal("TextContent");
                        var DateIndex = reader.GetOrdinal("TheDate");
                        var DeletedIndex = reader.GetOrdinal("Deleted");

                        return new OneTopic
                        {
                            TopicID = topicID,
                            UserID = reader.GetInt32(UserIDIndex),
                            Author = sanitize.DecodeText(reader.GetString(AuthorIndex)),
                            Header = sanitize.DecodeText(reader.GetString(HeaderIndex)),
                            TextContent = sanitize.DecodeText(reader.GetString(TextContentIndex)),
                            Date = sanitize.DecodeText(string.Format("{0}",reader.GetDateTime(DateIndex))),
                            Deleted = sanitize.DecodeText(reader.GetString(DeletedIndex)),
                        };
                    }
                }
                // kastar inget error om det inte finns någon.
                return null;
            }
            catch
            {
                throw new ApplicationException("an error has occured in the data access layer");
            }
        }
    }



    public void UpdateTopicContentOnTopicID(OneTopic topic)
    {
        using (SqlConnection conn = CreateConnection()) // using för att den alltid ska stängas sen.
        {
            try
            {
                var cmd = new SqlCommand("dbo.usp_UpdateTopicContentOnTopicID", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@TopicID", SqlDbType.Int, 4).Value = topic.TopicID;
                cmd.Parameters.Add("@TextContent", SqlDbType.VarChar, 2000).Value = topic.TextContent;
                cmd.Parameters.Add("@Header", SqlDbType.VarChar, 150).Value = topic.Header;

                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch
            {
                throw new ApplicationException("An error has occured in the data access layer.");
            }
        }
    }
}